package com.geyue.controller;

import com.geyue.common.toolbox.result.Res;
import com.geyue.core.model.Business;
import com.geyue.core.service.BusinessService;
import com.geyue.core.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Lyon on 2017/5/16.
 */
@Controller
@RequestMapping("/business")
public class BusinessController {

    @Autowired
    private BusinessService businessService;
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String list() {
        return "business/list";
    }

    @RequestMapping(value = "/", method = RequestMethod.POST)
    @ResponseBody
    public Res list(Business business, Integer pageSize, Integer pageNum) {
        business.setIsOk(Business.submit);
        return Res.success("", businessService.getPage(pageNum, pageSize, business));
    }

    @RequestMapping(value = "/ok", method = RequestMethod.GET)
    public String ok() {
        return "business/ok";
    }

    @RequestMapping(value = "/ok", method = RequestMethod.PUT)
    @ResponseBody
    public Res ok(Integer id) {
        Business business = new Business();
        business.setId(id);
        business.setIsOk(Business.ok);
        if (businessService.updateNotEmpty(business) > 0) {
//            User user = userService.getOneByKey(business.getUid());
//            User param = new User();
//            param.setId(user.getId());
//            param.setIntegral(user.getIntegral() + business.getIntegral());
//            if (userService.updateNotEmpty(param) > 0)
                return Res.success("审核成功!");
        }

        return Res.fail("审核失败");
    }

    @RequestMapping(value = "/no", method = RequestMethod.GET)
    public String no() {
        return "business/no";
    }

    @RequestMapping(value = "/no", method = RequestMethod.PUT)
    @ResponseBody
    public Res no(Business business) {

        business.setIsOk(Business.no);
        if (businessService.updateNotEmpty(business) > 0) {
                return Res.success("审核成功!");
        }

        return Res.fail("审核失败");
    }

    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public String excel() {
        return "business/export";
    }

    /**
     * 列表导出
     *
     * @param request
     */
    @RequestMapping(value = "excel")
    @ResponseBody
    public void excel(HttpServletRequest request, HttpServletResponse response,String start ,String end) {

        businessService.export(request,response,start,end);
    }

    /**
     * 读取excel
     * @param request
     * @param response
     * @param file
     */
    @RequestMapping(value = "read")
    @ResponseBody
    public Res excel(HttpServletRequest request, HttpServletResponse response, MultipartFile file) {
        return businessService.readExcel(file);
    }

}
